package com.zhiyou.service.impl;

import com.zhiyou.bean.Product;
import com.zhiyou.bean.User;
import com.zhiyou.dao.DianzanMapper;
import com.zhiyou.dao.UpmMapper;
import com.zhiyou.dao.UserMapper;
import com.zhiyou.service.UserService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{

    @Resource
    UserMapper userMapper;

    @Override
    public User getUser(User user) {
        User u = userMapper.selectOne(user);
        return userMapper.selectOne(user);
    }

    public User getUserByPhone(String phone) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone",phone);
        return userMapper.selectOneByExample(example);
    }

    @Override
    public int updateUserPwd(User user) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone",user.getPhone());
        return userMapper.updateByExampleSelective(user,example);

    }

    @Override
    public int identifyUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }
    public List<User> getAllUser() {
      return userMapper.selectAll();
    }

    @Override
    public Integer  updateUserById(User user) {
    return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int deleteUserById(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }


    @Override
    public List<Product> getUserMoney() {
        return userMapper.getUserMoney();
    }

    @Override
    public List<User> getUserByName(String username ) {

        return userMapper.getUserByName(username);
    }

    @Override
    public List<Product> getUserAndProductByName(String username) {
        return this.userMapper.getUserAndProductByName(username);
    }

    @Override
    public List<User> getUserByNameAudit(String realname,int i) {
        return userMapper.getUserByNameAudit(realname,i);
    }


    @Override
    public List<User> getAllUserAudit(int i) {
        Example example=new Example(User.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("status",i);
        return userMapper.selectByExample(example);
    }

    @Override
    public int auditUser(Integer id,int status ) {
        return userMapper.auditUser(id,status);
    }

    @Override
    public List<User> login(String email, String password){
        Example user = new Example(User.class);
        Example.Criteria criteria = user.createCriteria();
        criteria.andEqualTo("email",email).andEqualTo("password",password);
        List<User> findAdmin = userMapper.selectByExample(user);
        System.out.println("email findAdmin = " + findAdmin);
        return findAdmin;
    }

    @Override
    public List<User> loginPhone(String phone, String password){
        Example user = new Example(User.class);
        Example.Criteria criteria = user.createCriteria();
        criteria.andEqualTo("phone",phone).andEqualTo("password",password);
        List<User> findAdmin = userMapper.selectByExample(user);
        System.out.println("phone findAdmin = " + findAdmin);
        return findAdmin;
    }


    //邮箱注册
    @Override
    public int zhuceEmail(String email, String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setEmail(email);
        user.setPassword(password);
        return userMapper.insertSelective(user);
    }
    //手机号注册
    @Override
    public int ZhucePhone(String Phone, String username, String password) {
        User user = new User();
        user.setUsername(username);
        user.setPhone(Phone);
        user.setPassword(password);
        return userMapper.insertSelective(user);
    }
    //邮箱查重
    @Override
    public List<User> emailchachong(String email) {
        Example user = new Example(User.class);
        Example.Criteria criteria = user.createCriteria();
        criteria.andEqualTo("email",email);
        List<User> findAdmin = userMapper.selectByExample(user);
        System.out.println("phone findAdmin = " + findAdmin);
        return findAdmin;
    }

    //    用户名查重
    @Override
    public List<User> usernamechachong(String username) {
        Example user = new Example(User.class);
        Example.Criteria criteria = user.createCriteria();
        criteria.andEqualTo("username",username);
        List<User> findAdmin = userMapper.selectByExample(user);
        System.out.println("phone findAdmin = " + findAdmin);
        return findAdmin;
    }

    //    手机号查重
    @Override
    public List<User> phonechachong(String phone) {
        Example user = new Example(User.class);
        Example.Criteria criteria = user.createCriteria();
        criteria.andEqualTo("phone",phone);
        List<User> findAdmin = userMapper.selectByExample(user);
        System.out.println("phone findAdmin = " + findAdmin);
        return findAdmin;
    }
}
